package com.lfy.fyadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lfy.fyadmin.dto.RoleQueryRequest;
import com.lfy.fyadmin.entity.Role;
import com.lfy.fyadmin.entity.UserRole;
import com.lfy.fyadmin.entity.RolePermission;
import com.lfy.fyadmin.service.RoleService;
import com.lfy.fyadmin.service.UserRoleService;
import com.lfy.fyadmin.service.RolePermissionService;
import com.lfy.fyadmin.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
* @author 53409
* @description 针对表【role(角色表)】的数据库操作Service实现
* @createDate 2025-07-23 21:51:27
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{

    @Autowired
    private UserRoleService userRoleService;
    
    @Autowired
    private RolePermissionService rolePermissionService;

    @Override
    public boolean updateSelective(Role role) {
        return this.baseMapper.updateSelective(role) > 0;
    }

    @Override
    public IPage<Role> pageWithConditions(RoleQueryRequest queryRequest) {
        Page<Role> page = new Page<>(queryRequest.getPageNum(), queryRequest.getPageSize());
        
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        
        // 关键词查询：角色名或角色代码模糊查询
        if (StringUtils.hasText(queryRequest.getKeyword())) {
            queryWrapper.and(wrapper -> wrapper
                .like(Role::getName, queryRequest.getKeyword())
                .or()
                .like(Role::getCode, queryRequest.getKeyword())
            );
        }
        
        // 状态查询
        if (queryRequest.getStatus() != null) {
            queryWrapper.eq(Role::getStatus, queryRequest.getStatus());
        }

        //角色id
        if (queryRequest.getRoleId() != null) {
            queryWrapper.eq(Role::getId, queryRequest.getRoleId());
        }
        
        // 按创建时间倒序排列
        queryWrapper.orderByDesc(Role::getCreateTime);
        
        return this.page(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteRoleWithRelations(Long roleId) {
        try {
            // 1. 删除用户角色关联关系
            userRoleService.lambdaUpdate()
                    .eq(UserRole::getRoleId, roleId)
                    .remove();

            // 2. 删除角色权限关联关系
            rolePermissionService.lambdaUpdate()
                    .eq(RolePermission::getRoleId, roleId)
                    .remove();

            // 3. 删除角色本身
            return this.removeById(roleId);
        } catch (Exception e) {
            throw new RuntimeException("删除角色及其关联资源失败", e);
        }
    }
}




